# This Python file uses the following encoding: utf-8
import json
import sys
from pathlib import Path
from secrets import token_bytes

from PySide6.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton, QLineEdit, QFileDialog, QProgressBar, \
    QComboBox, QLabel
from utils import sm4_cbc_encrypt, sm4_cbc_decrypt
import uuid

class Widget(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.key = None
        self.decrypted = None
        self.line_label3 = None
        self.encrypt = None
        self.dencrypt_file_btn = None
        self.encrypt_file_btn = None
        self.pass_edit = None
        self.key_edit = None
        self.line_label2 = None
        self.line_label = None
        self.line_file_edit = None
        self.qcomboBox = None
        self.keyPub = {}
        self.fileChoosePaths = ""
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.initUI()

    def initUI(self):
        self.resize(640, 480)
        self.setWindowTitle('TongSuo')
        self.setFixedSize(640, 480)
        self.qcomboBox = QComboBox(self.centralWidget)
        self.qcomboBox.move(500, 150)
        self.qcomboBox.addItems(['sm4'])

        self.line_file_edit = QLineEdit(self.centralWidget)
        self.line_file_edit.resize(300, 30)
        self.line_file_edit.move(180, 150)
        self.line_file_edit.setPlaceholderText('输入需加密的内容')

        self.line_label = QLabel(self.centralWidget)
        self.line_label.move(130, 155)
        self.line_label.setText('原文:')

        self.key_edit = QLineEdit(self.centralWidget)
        self.key_edit.displayText()
        self.key_edit.resize(300, 30)
        self.key_edit.setPlaceholderText('可自动生成或者手动输入密钥')
        self.key_edit.move(180, 100)
        self.line_label2 = QLabel(self.centralWidget)
        self.line_label2.move(130, 105)
        self.line_label2.setText('密钥:')

        self.pass_edit = QLineEdit(self.centralWidget)
        self.pass_edit.displayText()
        self.pass_edit.resize(300, 30)
        self.pass_edit.move(180, 200)
        self.line_label3 = QLabel(self.centralWidget)
        self.line_label3.move(130, 205)
        self.line_label3.setText('密文:')

        self.dpass_edit = QLineEdit(self.centralWidget)
        self.dpass_edit.displayText()
        self.dpass_edit.resize(300, 30)
        self.dpass_edit.move(180, 250)
        self.line_label4 = QLabel(self.centralWidget)
        self.line_label4.move(130, 255)
        self.line_label4.setText('解密后:')

        self.encrypt_file_btn = QPushButton('开始加密', self.centralWidget)
        self.encrypt_file_btn.clicked.connect(self.encrypt_fun)
        self.encrypt_file_btn.move(180, 400)

        self.dencrypt_file_btn = QPushButton('开始解密', self.centralWidget)
        self.dencrypt_file_btn.clicked.connect(self.decrypt_fun)
        self.dencrypt_file_btn.move(380, 400)
        pass

    # 加密
    def encrypt_fun(self):
        # 生成32位16进制随机数
        if self.key_edit.text() != '':
            self.key = self.key_edit.text()
        else:
            self.key = uuid.uuid4().hex
        self.key_edit.setText(self.key)
        if self.qcomboBox.currentText() == 'sm4':
            self.encrypt = sm4_cbc_encrypt(self.line_file_edit.text(), self.key)
        self.pass_edit.setText(str(self.encrypt['output']))
        pass

    # 解密文件
    def decrypt_fun(self):
        if self.qcomboBox.currentText() == 'sm4':
            self.decrypted = sm4_cbc_decrypt(self.pass_edit.text(), self.key_edit.text())
        decrypted = str(self.decrypted['output'])
        self.dpass_edit.setText(decrypted)
        pass


if __name__ == "__main__":
    app = QApplication([])
    window = Widget()
    window.show()
    while app.exec():
        pass
